import os
import pygame
import random
import time

pygame.init()
enemy_hp = {"A": 1, "B": 2, "C": 3}

screen_width = 1000
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
#screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption("2D Game")
start_time = time.time()

player_image_left= pygame.image.load("C://Users/admin/Documents/Visual Studio 2022/pygametext1-master/pygametext1-master/PythonApplication1/PythonApplication1/acc/mainleft.png")
player_image_right  = pygame.image.load("C://Users/admin/Documents/Visual Studio 2022/pygametext1-master/pygametext1-master/PythonApplication1/PythonApplication1/acc/mainrighter.png")
enemy_a_image = pygame.image.load("C://Users/admin/Documents/Visual Studio 2022/pygametext1-master/pygametext1-master/PythonApplication1/PythonApplication1/acc/AAA.png")
enemy_b_image = pygame.image.load("C://Users/admin/Documents/Visual Studio 2022/pygametext1-master/pygametext1-master/PythonApplication1/PythonApplication1/acc/BBB.png")
enemy_c_image = pygame.image.load("C://Users/admin/Documents/Visual Studio 2022/pygametext1-master/pygametext1-master/PythonApplication1/PythonApplication1/acc/CCC.png")
background_image = pygame.image.load("C://Users/admin/Documents/Visual Studio 2022/pygametext1-master/pygametext1-master/PythonApplication1/PythonApplication1/acc/bg.png")
#background_image = pygame.transform.scale(background_image, (screen_width, screen_height))

player_image_left= pygame.transform.scale(player_image_left, (int(screen_width/10), int(screen_height/10)))
player_image_right= pygame.transform.scale(player_image_right, (int(screen_width/10), int(screen_height/10)))


enemy_a_image= pygame.transform.scale(enemy_a_image, (int(screen_width/10), int(screen_height/10)))
enemy_b_image= pygame.transform.scale(enemy_b_image, (int(screen_width/10), int(screen_height/10)))
enemy_c_image= pygame.transform.scale(enemy_c_image, (int(screen_width/10), int(screen_height/10)))

pygame.mixer.music.load("C://Users/admin/Documents/Visual Studio 2022/pygametext1-master/pygametext1-master/PythonApplication1/PythonApplication1/acc/a.mp3")
pygame.mixer.music.play(-1)


class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = player_image_left
        self.rect = self.image.get_rect()
        self.rect.centerx = screen_width // 2
        self.rect.bottom = screen_height - 50
        self.speed = 4
        self.lives = 3
        self.score = 0

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.rect.x -= self.speed
            self.image = player_image_left
        elif keys[pygame.K_RIGHT]:
            self.rect.x += self.speed
            self.image = player_image_right
        if keys[pygame.K_UP]:
            self.rect.y -= self.speed
        if keys[pygame.K_DOWN]:
            self.rect.y += self.speed

        self.rect.clamp_ip(screen.get_rect())

class Enemy(pygame.sprite.Sprite):
    def __init__(self, enemy_type):
        super().__init__()
        self.enemy_type = enemy_type
        if enemy_type == "A":
            self.image = enemy_a_image
        elif enemy_type == "B":
            self.image = enemy_b_image
        elif enemy_type == "C":
            self.image = enemy_c_image
        self.rect = self.image.get_rect()
        self.rect.x = random.choice([-self.rect.width, screen_width])
        self.rect.y = screen_height - 150
        self.speed = random.randint(3, 5)
        self.hp = enemy_hp[enemy_type]
        
    def update(self):
        

        if self.rect.colliderect(player.rect):
            player.lives -= 1
            self.kill()
        else:
            if self.rect.x < player.rect.x:
                self.rect.x += self.speed
            else:
                self.rect.x -= self.speed

            if self.rect.y < player.rect.y:
                self.rect.y += self.speed
            else:
                self.rect.y -= self.speed
                

class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y, direction):
        super().__init__()
        self.image = pygame.Surface((5, 10))
        self.image.fill((255, 255, 255))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.speed = 10
        self.direction = direction

    def update(self):
        if self.direction == "left":
            self.rect.x -= self.speed
        else:
            self.rect.x += self.speed

        if self.rect.x < 0 or self.rect.x > screen_width:
            self.kill()

player = Player()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE]:
        if player.image == player_image_left:
            bullet = Bullet(player.rect.centerx - player.rect.width//2, player.rect.top, "left")
        else:
            bullet = Bullet(player.rect.centerx + player.rect.width//2, player.rect.top, "right")
        all_sprites.add(bullet)
        bullets.add(bullet)

    all_sprites.update()

    for bullet in bullets:
        hit_list = pygame.sprite.spritecollide(bullet, enemies, False)
        for enemy in hit_list:
            enemy.hp -= 1
            bullet.kill()
            if enemy.hp <= 0:
                player.score += 1
                enemy.kill()
        if bullet.rect.y < 0:
            bullet.kill()
    if random.random() < 0.05:
        enemy_type = random.choice(["A", "B", "C"])
        enemy = Enemy(enemy_type)
        all_sprites.add(enemy)
        enemies.add(enemy)

    hit_list = pygame.sprite.spritecollide(player, enemies, False)
    for enemy in hit_list:
        player.lives -= 1
        enemy.kill()
        if player.lives <= 0:
            print("gameover")
            player.score += 1
            end_time = time.time()
            game_time = end_time - start_time

            pygame.quit()
            pygame.init()
            screen = pygame.display.set_mode((400, 200))
            pygame.display.set_caption("gameover")
            font = pygame.font.Font(None, 36)
            text = font.render(f"killed: {player.score}", True, (255, 255, 255))
            time_text = font.render(f"gametime: {game_time:.2f}s", True, (255, 255, 255))
            screen.blit(text, (50, 50))
            screen.blit(time_text, (50, 100))
            pygame.display.flip()

            while True:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                    elif event.type == pygame.MOUSEBUTTONDOWN:
                        pygame.quit()
                        os.system("python C://Users/admin/Documents/Visual Studio 2022/pygametext1-master/pygametext1-master/PythonApplication1/PythonApplication1/main.py")
            #running = False

    screen.blit(background_image, (0, 0))
    all_sprites.draw(screen)
    font = pygame.font.Font(None, 36)
    text = font.render("Score: " + str(player.score), True, (255, 255, 255))
    screen.blit(text, (10, 10))

    text = font.render("Lives: " + str(player.lives), True, (255, 255, 255))
    screen.blit(text, (screen_width - text.get_width() - 10, 10))
    pygame.display.flip()
    
    pygame.time.delay(50)

pygame.quit()
